var DHLYResArray = [];
var DXResArray = [];
var CZResArray = [];
var msgServIp = "127.0.0.1";
//////////////////////////电话调度////////////////////////////////////////////////


/////////////////////////电话录音管理/////////////////////////////////////////////
function queryDhlygl_Yjtx(){
	var timeFrom = $("#dhlyTimeFrom").val();
	var timeTo = $("#dhlyTimeTo").val();
	var dhNum = $("#dhlyNum").val();
	var lx = $("#dhlyLX").val();
	var url = "queryYjtx.shtml";
	var data = {
		"timeFrom":timeFrom,
		"timeTo":timeTo,
		"dhNum":dhNum,
		"lx":lx,
	};
	DHLYResArray = [];
	$('#tblDHLYGL').datagrid({
		url : url,
		queryParams : data,
		onLoadSuccess : function(res) {
		},
		onLoadError : function(res) {
			alert("查询失败!");
		}
	});
}

function inittblDHLYGL(){
	$("#tblDHLYGL").datagrid({
		url:"",
		striped : true,
		pageSize : 10,
		height : 315,
		nowrap : false,
		columns : [[ {
					field : 'yjtx01010',
					title : '分机号',
					width : 45,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01040',
					title : '来\去电类型',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						var value = "";
						if(rec.yjtx01040=="1"){
							value = "来电";
						}else{
							value = "去电";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+value
								+"</div>";
						return resutsHTML;
					}
				},{
					field : 'yjtx01020',
					title : '来\去电时间',
					width : 160,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01030',
					title : '来\去电号码',
					width : 100,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01120',
					title : '接报人员',
					width : 60,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01060',
					title : '是否回拨',
					width : 60,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						var value = "";
						if(rec.yjtx01060=="1"){
							value = "已回拨";
						}else{
							value = "未回拨";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+value
								+"</div>";
						return resutsHTML;
					}
				},{
					field : 'yjtx01100',
					title : '报送人员',
					width : 60,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01070',
					title : '录音播放',
					width : 60,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						// 该对象
						//curRecObj = rec;
						var num = DHLYResArray.length;
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='bofangDHLYFile(\""
								+ num
								+ "\",false)'>"+rec.yjtx01070+"</a>"
								+"</div>";
						return resutsHTML;
					}
				},{
					field : 'oper',
					title : '操作',
					width : 80,
					align : 'center',
					formatter : function(value, rec, rowIndex) {// value id rec
						// 该对象
						//curRecObj = rec;
						DHLYResArray.push(rec);
						var num = DHLYResArray.length-1;
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='showDHLYDetails(\""
								+ num
								+ "\",false)'>详情</a>";
						return resutsHTML;
					}
				}]],
		pagination : true,
		rownumbers : true,
		singleSelect : true,
		onClickRow : function(rowIndex) {
		},
	});
}

function bofangDHLYFile(num){
	var fileName = DHLYResArray[num].yjtx01070;
	var fileUrl = "../../uploadfolder/mp3folder/"+fileName;
	window.open("../filePlay.jsp?url="+fileName);
}

function showDHLYDetails(index){
	var obj = DHLYResArray[index];
	var dhlyFJH = obj.yjtx01010;
	var dhlyLQDSJ = obj.yjtx01020;
	var dhlyLQDLX = obj.yjtx01040=="1"?"来电":"去电";
	var dhlyLQDHM = obj.yjtx01030;
	var dhlyJTSB = obj.yjtx01050=="1"?"已接":"未接";
	var dhlySFHB = obj.yjtx01060=="1"?"已回拨":"未回拨";
	var dhlyBSRY = obj.yjtx01100;
	var dhlyBSDW = obj.yjtx01110;
	var dhlyJSRY = obj.yjtx01120;
	var dhlyJSDW = obj.yjtx01130;
	var dhlyBZ = obj.yjtx01140;
	$("#dhlyFJH").attr("value",dhlyFJH).attr("readOnly","readOnly");
	$("#dhlyLQDSJ").attr("value",dhlyLQDSJ).attr("readOnly","readOnly");
	$("#dhlyLQDLX").attr("value",dhlyLQDLX).attr("readOnly","readOnly");
	$("#dhlyLQDHM").attr("value",dhlyLQDHM).attr("readOnly","readOnly");
	$("#dhlyJTSB").attr("value",dhlyJTSB).attr("readOnly","readOnly");
	$("#dhlySFHB").attr("value",dhlySFHB).attr("readOnly","readOnly");
	$("#dhlyBSRY").attr("value",dhlyBSRY).attr("readOnly","readOnly");
	$("#dhlyBSDW").attr("value",dhlyBSDW).attr("readOnly","readOnly");
	$("#dhlyJSRY").attr("value",dhlyJSRY).attr("readOnly","readOnly");
	$("#dhlyJSDW").attr("value",dhlyJSDW).attr("readOnly","readOnly");
	$("#dhlyBZ").attr("value",dhlyBZ).attr("readOnly","readOnly");
	$("#DHLY").dialog("open");
}

/////////////////////////短信管理////////////////////////////////////////////
function queryDxgl_Yjtx(){
	var timeFrom = $("#dxTimeFrom").val();
	var timeTo = $("#dxTimeTo").val();
	var url = "queryYjtx.shtml";
	var data = {
		"timeFrom":timeFrom,
		"timeTo":timeTo,
		"lx":"3",
		"dhNum":"",
	};
	DXResArray = [];
	$('#tblDXGL').datagrid({
		url : url,
		queryParams : data,
		onLoadSuccess : function(res) {
		},
		onLoadError : function(res) {
			alert("查询失败!");
		}
	});
}

function inittblDXGL(){
	$("#tblDXGL").datagrid({
		url:"",
		striped : true,
		pageSize : 10,
		height : 315,
		nowrap : false,
		columns : [[ {
					field : 'yjtx01020',
					title : '时间',
					width : 160,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01030',
					title : '来电号码',
					width : 80,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01120',
					title : '接报人员',
					width : 80,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01130',
					title : '接报单位',
					width : 120,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01100',
					title : '报送人员',
					width : 80,
					align : 'center',
					editor : 'text'
				},{
					field : 'yjtx01110',
					title : '报送人员单位',
					width : 120,
					align : 'center',
					editor : 'text'
				},{
					field : 'oper',
					title : '操作',
					width : 120,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						// 该对象
						//curRecObj = rec;
						DXResArray.push(rec);
						var num = DXResArray.length-1;
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='showDXNR(\""
								+ num
								+ "\",false)'>查看短信信息</a>"
								+"</div>";
						return resutsHTML;
					}
				}]],
		pagination : true,
		rownumbers : true,
		singleSelect : true,
		onClickRow : function(rowIndex) {
		},
	});
}


function showDXNR(index){
	var dxnr = DXResArray[index].yjtx01080;
	$("#DXNRText").attr("value",dxnr).attr("readOnly","readOnly");
	$("#DXNR").dialog("open");
}
/////////////////////////传真管理////////////////////////////////////////////
function queryCzgl_Yjtx(){
	var timeFrom = $("#czTimeFrom").val();
	var timeTo = $("#czTimeTo").val();
	var url = "queryYjtx.shtml";
	var data = {
		"timeFrom":timeFrom,
		"timeTo":timeTo,
		"lx":"4",
		"dhNum":"",
	};
	CZResArray = [];
	$('#tblCZGL').datagrid({
		url : url,
		queryParams : data,
		onLoadSuccess : function(res) {
		},
		onLoadError : function(res) {
			alert("查询失败!");
		}
	});
}

function inittblCZGL(){
	$("#tblCZGL").datagrid({
		url:"",
		striped : true,
		pageSize : 10,
		height : 315,
		nowrap : false,
		columns : [[  {
			field : 'yjtx01020',
			title : '时间',
			width : 160,
			align : 'center',
			editor : 'text'
		},{
			field : 'yjtx01030',
			title : '来电号码',
			width : 80,
			align : 'center',
			editor : 'text'
		},{
			field : 'yjtx01120',
			title : '接报人员',
			width : 80,
			align : 'center',
			editor : 'text'
		},{
			field : 'yjtx01130',
			title : '接报单位',
			width : 120,
			align : 'center',
			editor : 'text'
		},{
			field : 'yjtx01100',
			title : '报送人员',
			width : 80,
			align : 'center',
			editor : 'text'
		},{
			field : 'yjtx01110',
			title : '报送人员单位',
			width : 120,
			align : 'center',
			editor : 'text'
		},{
					field : 'oper',
					title : '操作',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						// 该对象
						//curRecObj = rec;
						CZResArray.push(rec);
						var num = CZResArray.length-1;
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='DownloadPDF(\""
								+ num
								+ "\",false)'>传真下载</a>"
								+"</div>";
						return resutsHTML;
					}
				}]],
		pagination : true,
		rownumbers : true,
		singleSelect : true,
		onClickRow : function(rowIndex) {
		},
	});
}

function DownloadPDF(index){
	
}

////////////////////////////////////短信发送///////////////////////////////////////////////////
function inittblDXFS(){
//	var data = [createNode()];
//	 var o = { showcheck: true
//	            };
//	 o.data = data;                  
//	 $("#DwRy_treeDiv").treeview(o);     
	debugger;
	//制造根节点
	 var root = {
			    "id" : "0",
			    "text" : "人员信息",
			    "value" : "",
			    "showcheck" : false,
			    complete : true,
			    "isexpand" : true,
			    "checkstate" : 0,
			    "hasChildren" : true,
	 };
	 
	//查询数据库，去搞湖北省和各个市，二级目录
	var erjiArray = [];
	var erjiUrl = "queryErjiMuluForYjtx.shtml";
	$.post(erjiUrl,null,function(res){
		res = eval(res);
		for(var i = 0;i<res.length;i++){
			var obj = {
				"id":"node-"+res[i].xzqh01000,
				"text":res[i].xzqh01020,
				"value":res[i].xzqh01010,
				"showcheck" : false,
			    complete : true,
			    "isexpand" : false,
			    "checkstate" : 0,
			    "hasChildren" : true
			};
			erjiArray.push(obj);
		}
		sanjiMuluCreate(root,erjiArray)
	});
}

//现在去搞3级，即各个单位
function sanjiMuluCreate(root,erjiArray){
	
	var bigSanjiArray = [];
	//通过for循环去请求市县下的单位
	var i = 0;
	ajaxForXunhuan(i);
	
	function ajaxForXunhuan(i){
		var sxbh = erjiArray[i].value;
		var url = "queryDWBySxbh.shtml";
		if(i==0){
			url ="queryHbsDw.shtml";
		}
		$.post(url,{"sxbh":sxbh},function(res){
			//
			var sanjiArray = [];
			res = eval(res);
			for(var j  = 0;j<res.length;j++){
				var obj = {
						"id":"node-"+res[j],
						"text":res[j],
						"value":res[j],
						"showcheck" : false,
					    complete : true,
					    "isexpand" : false,
					    "checkstate" : 0,
					    "hasChildren" : true
				};
				sanjiArray.push(obj);
			}
			bigSanjiArray.push(sanjiArray);
			i = i+1;
			if(i<erjiArray.length){
				ajaxForXunhuan(i);
			}else{
				sijiMuluCreate(root,erjiArray,bigSanjiArray);
			}
		});
	}
}


//现在搞四级
function sijiMuluCreate(root,erjiArray,bigSanjiArray){
	 //bigSanjiArray,里面存储的是数组，每个数组代表每个代为，bigSanjiArray的index代表的省市
	//bigSanjiArray的位数必定与erjiArray是一致的
	//erjiArray[i]["ChildNodes"] = bigSanjiArray[i];
	var i = 0;
	ajaxForRyBigXunhuan(i);
	
	//这个回调要把人搞死
	function ajaxForRyBigXunhuan(i){
		var sanjiArray = bigSanjiArray[i];
		var j = 0;
		if(sanjiArray.length>0){
			ajaxForRySmallXunhuan(j);
		}else{
			ajaxForRyBigXunhuan(i+1);
		}
		function ajaxForRySmallXunhuan(j){
			var dwmc = sanjiArray[j].value;
			var url = "queryRyDhByDwmc.shtml";
			$.post(url,{dwmc:dwmc},function(res){
				var sijiArray = [];
				res = eval(res);
				for(var k = 0;k<res.length;k++){
					var obj = {
							"id":"node-"+res[k][1],
							"text":res[k][0],
							"value":res[k][1],
							"showcheck" : true,
						     complete : true,
						    "isexpand" : true,
						    "checkstate" : 0,
						    "hasChildren" : false
					};
					sijiArray.push(obj);
				}
				sanjiArray[j]["ChildNodes"] = sijiArray;
				j = j+1;
				if(j<sanjiArray.length){
					ajaxForRySmallXunhuan(j);
				}else{
					erjiArray[i]["ChildNodes"] = sanjiArray;
					i = i+1;
					if(i<erjiArray.length){
						ajaxForRyBigXunhuan(i);
					}else{
						createAllDWRYTree(root,erjiArray);
					}
				}
			})
		}
	}
	
}

//把树全部搞出来了
function createAllDWRYTree(root,erjiArray){
	root["ChildNodes"] = erjiArray;
	var data = [root];
	 var o = { showcheck: true,
			 	oncheckboxclick:onTreeCheckBoxClick
	            };
	 o.data = data;                  
	 $("#DwRy_treeDiv").treeview(o);  
}

//点击某个人的checkbox的时候
function onTreeCheckBoxClick(item,flag){
	var nameStr = item.text;
	var dhhmStr = item.value;
	if(flag==0){//代表取消选中
		var nameArray = $("#sxrNameDiv").val().split(";");
		var dhhmArray = $("#sxrDhhmDiv").val().split(";");
		nameArray = removeArrayItemByItem(nameStr,nameArray);
		dhhmArray = removeArrayItemByItem(dhhmStr,dhhmArray);
		$("#sxrNameDiv").html("");
		$("#sxrDhhmDiv").html("");
		nameStr = nameArray.join(";");
		dhhmStr = dhhmArray.join(";");
		$("#sxrNameDiv").append(nameStr);
		$("#sxrDhhmDiv").append(dhhmStr);
	}else{
		$("#sxrNameDiv").append(nameStr+";");
		$("#sxrDhhmDiv").append(dhhmStr+";");
	}
}

//点击发送时候的按钮事件
function clickFasongButtonFunc(){
	var dhhmStr = $("#sxrDhhmDiv").val();
	var dhhmArray = dhhmStr.split(";");
	dhhmArray = top.removeArrayItemByIndex(dhhmArray.length-1,dhhmArray);
	var dhhmstr = dhhmArray.join(",");
	var dxnrStr = $("#duanxinEvent").val()+":"+$("#dxnrDiv").val();
	var url = "fasongDuanxinDiaoyong.shtml";
	var fasongUrl = "http://"+msgServIp+"/sendMessageServices/services.ashx?mobiles="+dhhmstr+"&strContent="+encodeURI(dxnrStr);
	$.post(url,{fasongUrl:fasongUrl},duanxinFasongCallback);
}

function duanxinFasongCallback(res){
	alert(res);
}
